package cn.itcast.health.provider.service.impl;

import cn.itcast.health.commons.entity.QueryPageBean;
import cn.itcast.health.commons.entity.Result;
import cn.itcast.health.commons.resources.MessageConstant;
import cn.itcast.health.interfaces.service.CheckGroupService;
import cn.itcast.health.mapper.CheckGroupMapper;
import cn.itcast.health.mapper.CheckitemMapper;
import cn.itcast.health.pojo.CheckGI;
import cn.itcast.health.pojo.CheckGroup;
import cn.itcast.health.pojo.CheckItem;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckitemMapper checkitemMapper;

    @Override
    public Result addCheckGroupAndCheckGroupCheckitem(CheckGI checkGI) {
        Result result = null;
        try{
            CheckGroup checkGroup = checkGI.getCheckGroup();
            Integer[] checkitems = checkGI.getCheckitems();

            checkGroupMapper.addCheckGroup(checkGroup);
            Integer checkGroupid = checkGroup.getId();
            if(checkGroupid != null){
                checkGroupMapper.addCheckGroupCheckitem(checkGroupid,checkitems);
            }
            result = new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            result = new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return result;
    }

    @Override
    public Result findByPageCheckGroupData(QueryPageBean queryPageBean) {
        Result result = null;
        try {
            //设置分页条件
            PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
            //查询数据
            List<CheckItem> checkItems = checkGroupMapper.findByPageCheckGroupData(queryPageBean.getQueryString());
            PageInfo pageInfo = new PageInfo(checkItems);
            Map<String,Object> map = new HashMap();
            map.put("list",pageInfo.getList());
            map.put("total",pageInfo.getTotal());
            result = new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,map);
        }catch (Exception e){
            result = new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return result;
    }

    @Override
    public Result findByIdCheckitemAnaIdsData(CheckGroup checkGroup) {
        Result result = null;
        try {
            HashMap<String, Object> map = new HashMap<>();
            //调用检查项的mapper 中的查询所有的检查项
            List<CheckItem> checkitemAll = checkitemMapper.findCheckitemAll();
            Integer[] checkitemIds = checkitemMapper.findByIdCheckitemAnaIdsData(checkGroup.getId());
            map.put("list",checkitemAll);
            map.put("checkitems",checkitemIds);
            result = new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,map);
        }catch (Exception e){
            result = new Result(true,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return result;
    }

    @Override
    public Result updateCheckGroupAndCheckGroupCheckitemData(CheckGI checkGI) {
        Result result = null;
        try{
            CheckGroup checkGroup = checkGI.getCheckGroup();
            Integer[] checkitems = checkGI.getCheckitems();
            //更新检查组的数据
            checkGroupMapper.updateCheckGroup(checkGroup);
            //删除关联表的数据 根据检查组的id
            checkGroupMapper.deleteByidCheckGroupCheckitem(checkGroup.getId());
            //添加中间表数据
            checkGroupMapper.addCheckGroupCheckitem(checkGroup.getId(),checkitems);
            result = new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            result = new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return result;
    }

    @Override
    public Result deleteByIdCheckGroupData(Integer id) {
        Result result = null;
        try {
            checkGroupMapper.deleteByIdCheckGroupData(id);
            result = new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            result = new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return result;
    }

    @Override
    public Result findCheckgroupAll() {
        Result result = null;
        try {
            List<CheckGroup>  checkGroups = checkGroupMapper.findCheckgroupAll();
            result = new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroups);
        }catch (Exception e){
            result = new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return result;
    }
}
